package alex.rsx.view;

import java.util.Vector;

import alex.rsx.activity.r.R;
import alex.rsx.data.MenuItem;
import alex.rsx.data.StaticData;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 * @author Alex.Lu
 *
 */
public class MainLeftAdapter extends BaseExpandableListAdapter{

	public Context mContext;
	public static Vector<MenuItem> mList;
	private LayoutInflater mInflater;
	private OnClickListener mOnClickListener;
	
	public MainLeftAdapter(Context context){
		mInflater = LayoutInflater.from(context);
		mList = StaticData.getMenuItem();
		mContext = context;
	}
	
	public void setOn(OnClickListener onClickListener){
		mOnClickListener = onClickListener;
	}
	
	
	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	// -----------------Child----------------//
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if(mList.get(groupPosition).list != null){
			return mList.get(groupPosition).list.size();
		}
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if(groupPosition==1){
			TextView textView = new TextView(mContext);
			textView.setPadding(20, 10, 10, 10);
			textView.setText(mList.get(groupPosition).list.get(childPosition).name);
			convertView = textView;
		}else if(groupPosition==2){
			LinearLayout v = new LinearLayout(mContext);
			v.setOrientation(LinearLayout.VERTICAL);
			
			LinearLayout h1 = new LinearLayout(mContext);
			Button c1 = new Button(mContext);
			c1.setOnClickListener(mOnClickListener);
			c1.setId(StaticData.getClassifys().get(0).tag);
			c1.setBackgroundResource(StaticData.getClassifys().get(0).icon);
			h1.addView(c1);
			
			Button c2 = new Button(mContext);
			c2.setOnClickListener(mOnClickListener);
			c2.setId(StaticData.getClassifys().get(1).tag);
			c2.setBackgroundResource(StaticData.getClassifys().get(1).icon);
			h1.addView(c2);
			v.addView(h1);
			
			LinearLayout h2 = new LinearLayout(mContext);
			Button c3 = new Button(mContext);
			c3.setOnClickListener(mOnClickListener);
			c3.setId(StaticData.getClassifys().get(2).tag);
			c3.setBackgroundResource(StaticData.getClassifys().get(2).icon);
			h2.addView(c3);
			
			Button c4 = new Button(mContext);
			c4.setOnClickListener(mOnClickListener);
			c4.setId(StaticData.getClassifys().get(2).tag);
			c4.setBackgroundResource(StaticData.getClassifys().get(3).icon);
			h2.addView(c4);
			v.addView(h2);
			convertView = v;
		}else if(groupPosition==3){
			TextView textView = new TextView(mContext);
			textView.setPadding(20, 10, 10, 10);
			textView.setBackgroundResource(mList.get(groupPosition).list.get(childPosition).icon);
			convertView = textView;
		}
		return convertView;
	}


	@Override
	public long getCombinedChildId(long groupId, long childId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long getCombinedGroupId(long groupId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		int tag = mList.get(groupPosition).tag;
		View view = mInflater.inflate(R.layout.main_left_adapter_item, null);
		ImageView groupname = (ImageView) view.findViewById(R.id.icon);
		TextView name = (TextView)view.findViewById(R.id.name);
		ImageView logo = (ImageView)view.findViewById(R.id.main_left_logo);
		ImageView arrow = (ImageView)view.findViewById(R.id.main_left_arrow);
		if(groupPosition==0){
			logo.setVisibility(View.VISIBLE);
			groupname.setVisibility(View.GONE);
			name.setVisibility(View.GONE);
			arrow.setVisibility(View.GONE);
		}else{
			logo.setVisibility(View.GONE);
			groupname.setVisibility(View.VISIBLE);
			name.setVisibility(View.VISIBLE);
			arrow.setVisibility(View.VISIBLE);
			
			name.setText(mList.get(groupPosition).name);
			groupname.setClickable(false);
			groupname.setImageResource(mList.get(groupPosition).icon);
			groupname.setBackgroundDrawable(null);
		}
		return view;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	

}
